package cn.hzw.supermarket.activity;

import static cn.hzw.supermarket.adapter.RecyclerViewAdapter.getDrawablePic;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;

import cn.hzw.supermarket.R;
import cn.hzw.supermarket.databinding.ActivityDetails1Binding;
import cn.hzw.supermarket.domain.Goods;
import cn.hzw.supermarket.model.GoodsResultInfo;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Details1Activity extends AppCompatActivity {

    private ActivityDetails1Binding binding;
    final OkHttpClient client = new OkHttpClient();
    public static Goods goods = new Goods();
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityDetails1Binding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.layoutTitle.tvTitle.setText("详情页面");
        binding.layoutTitle.ivBack.setOnClickListener(view -> {
            finish();
        });
        if (Build.VERSION.SDK_INT>9){
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        Drawable drawable = getDrawablePic(bundle.getString("pic"));
        binding.goodsPic.setImageDrawable(drawable);

//        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
//        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedClosableObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
        Log.d("TAG5", "initView: d"+bundle.getString("pic"));
        binding.goodsName.setText(bundle.getString("name"));
        binding.goodsPrice.setText(bundle.getString("price"));
        binding.goodsContent.setText(bundle.getString("content"));
        binding.add.setOnClickListener(view -> {
            postRequest(bundle.getString("name"),LoginActivity.user.getUsername());
            Toast.makeText(getApplicationContext(), "添加成功", Toast.LENGTH_SHORT).show();
        });

    }

    private void postRequest(String goodsname, String username) {
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        JSONObject userJSONObj = new JSONObject();
        try {
            userJSONObj.put("goodsname",goodsname);
            userJSONObj.put("username",username);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //建立请求表单，添加上传服务器的参数
        RequestBody userMessage = RequestBody.create(JSON,""+userJSONObj);
        //发起请求
        Request request = new Request.Builder()
                .url("http://10.0.2.2:8080/goodsinfo/selectbyname")
                .post(userMessage)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e){
                System.out.println(e);
                System.out.println("连接失败");
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String goodsresult = response.body().string();//接收从后端传递过来的JSON字符串
                GoodsResultInfo goodsresultInfo = new GoodsResultInfo();
                goodsresultInfo = com.alibaba.fastjson.JSONObject.parseObject(goodsresult,GoodsResultInfo.class);//将JSON字符串转换成对象

                goods = goodsresultInfo.getGoods();
                //System.out.println(goodsresult);
            }
        });
    }
}